import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import vueJsx from '@vitejs/plugin-vue-jsx';
import vueI18n from '@intlify/vite-plugin-vue-i18n';
import svgLoader from 'vite-svg-loader';
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers';
import IconsResolver from 'unplugin-icons/resolver';
import Icons from 'unplugin-icons/vite';

import PathHelper from './PathHelper';

// https://vitejs.dev/config/
export default defineConfig({
  base: '/',
  server: {
    host: 'local-cns.test.ctyun.cn',
    port: 7200,
    cors: true,
    origin: 'http://local-cns.test.ctyun.cn:7200',
    fs: {
      allow: [PathHelper.getPathAtRoot()],
      deny: [
        '.env',
        '.env.*',
        '*.{pem,crt}',
        PathHelper.getPathRelativeToRoot('./subApps'),
      ],
    },
  },
  plugins: [
    vue(),
    vueJsx(),
    vueI18n({}),
    svgLoader(),
    AutoImport({
      dts: true,
      imports: [
        'vue',
        'vue-router',
        'vue-i18n',
      ],
      resolvers: [
        ElementPlusResolver(),
        IconsResolver({ prefix: 'Icon' }),
      ],
    }),
    Components({
      dts: true,
      resolvers: [
        ElementPlusResolver({ importStyle: 'sass' }),
        IconsResolver({ enabledCollections: ['ep'] }),
      ],
    }),
    Icons({ autoInstall: true }),
  ],
  resolve: {
    alias: {
      '@': PathHelper.getPathRelativeToRoot('./core'),
      '@core': PathHelper.getPathRelativeToRoot('./core/src'),
    },
  },
  define: {
    'process.env.API_ENV': `'${process.env.API_ENV}'`,
  },
  css: {
    preprocessorOptions: {
      scss: { additionalData: '@use "@core/styles/element-sub-app.scss" as *;' },
    },
  },
});
